package jee.boot.module.oauth2.auth.verification;

import jee.boot.common.basic.result.MessageCodeHelper;
import jee.boot.entity.auth.Account;
import jee.boot.common.utils.SpringContextHolder;
import jee.boot.common.utils.StringUtils;

import java.util.Date;

/**
 * 密码过期验证
 */
public class PasswordExpiredVerification implements AccountVerificationFilter {

    /**
     * 验证是否通过
     *
     * @param context
     * @return 抛出异常即为不通过, 终止后面的验证
     */
    @Override
    public void verification(AccountVerificationContext context) throws AccountVerificationException {
        Account account = context.getAccount();
        if (StringUtils.equals("account_status_3", account.getAccountStatus()) || (account.getPasswordInvalidDate() != null && new Date().after(account.getPasswordInvalidDate()))) {
            throw new AccountVerificationException(getMessageCodeHelper().getMessage(1108));
        }
    }

    public MessageCodeHelper getMessageCodeHelper() {
        return SpringContextHolder.getApplicationContext().getBean(MessageCodeHelper.class);
    }
}
